from django.shortcuts import render,redirect,HttpResponse
from django import forms
from io import BytesIO

from app01 import models
from app01.utils.form import LoginForm
from app01.utils.encrypt import md5
from app01.utils.code import check_code

def login(request):
    """ 登录 """    
    if request.method == "GET":
        # return render(request, 'login.html')
        form = LoginForm()
        return render(request, 'login.html', {'form': form})
    
    form = LoginForm(data=request.POST)
    if form.is_valid():       
        # 验证码的校验
        user_input_code = form.cleaned_data.pop('code')
        code = request.session.get('image_code', "")
        if code.upper() != user_input_code.upper():
            form.add_error("code", "验证码错误")
            return render(request, 'login.html', {'form': form})
        
        # 验证成功
        #    print(form.cleaned_data)
        # admin_object = models.Admin.objects.filter(username = form.cleaned_data['username'],password='').first()
        admin_object = models.Admin.objects.filter(**form.cleaned_data).first()
        if not admin_object:
            form.add_error('password','用户名或密码错误')
            return render(request, 'login.html', {'form': form})
        request.session['info'] = {'id':admin_object.id,'name': admin_object.username}
        request.session.set_expiry(60 * 60 * 24 * 7)
        return redirect('/admin/list/')
    return render(request, 'login.html', {'form': form})

def logout(request):
    """ 注销 """    
    request.session.clear()
    return redirect('/logout/')

def image_code(request):
    """ 生成图片验证码 """    
    img,code_string = check_code()
    # 写入到自己的session中（以便于后续获取验证码再进行校验）
    request.session['image_code'] = code_string
    # 给Session设置60s超时
    request.session.set_expiry(60)
    stream = BytesIO()
    img.save(stream,'png')
    return HttpResponse(stream.getvalue())
        